package com.mes.cloud.web;

import com.github.pagehelper.PageInfo;

import com.mes.cloud.common.CommPager;
import com.mes.cloud.dto.QuartzTaskDTO;
import com.mes.cloud.quartz.QuartzPlugin;
import com.mes.cloud.service.QuartzTaskService;
import com.mes.cloud.utils.JSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.JobKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 定时任务
 *
 * @author wxm
 * @Time 2019-6-10
 */
@Api(tags = "QuartzTaskController", description = "定时任务")
@RestController
@RequestMapping("/quartzTask")
public class QuartzTaskController {
    private static final Logger LOGGER = LoggerFactory.getLogger(QuartzTaskController.class);

    @Autowired
    private QuartzTaskService quartzTaskService;

    // 启动任务
    @ApiOperation(value = "启动任务")
    @RequestMapping(value = "/start", method = RequestMethod.POST)
    public JSONResult start(@RequestBody QuartzTaskDTO quartzTaskDTO) {
        JSONResult result = null;
        try {
            QuartzTaskDTO quartzTaskDTOTemp = quartzTaskService.getQuartzTaskById(quartzTaskDTO.getId());
            String className = quartzTaskDTOTemp.getClazz();
            // 恢复任务
            JobKey jobKey = JobKey.jobKey(className, className);
            QuartzPlugin.scheduler.resumeJob(jobKey);
            quartzTaskService.updateQuartzTaskState(quartzTaskDTO.getId(), QuartzTaskDTO.STATE_START);
            result = JSONResult.success();
        } catch (Exception e) {
            LOGGER.error(e.toString());
            result = JSONResult.fail(201, e.toString());
        }
        return result;
    }

    // 暂停任务
    @ApiOperation(value = "暂停任务")
    @RequestMapping(value = "/stop", method = RequestMethod.POST)
    public JSONResult stop(@RequestBody QuartzTaskDTO quartzTaskDTO) {
        JSONResult result = null;
        try {
            QuartzTaskDTO quartzTaskDTOTemp = quartzTaskService.getQuartzTaskById(quartzTaskDTO.getId());
            String className = quartzTaskDTOTemp.getClazz();

            // 暂停任务
            JobKey jobKey = JobKey.jobKey(className, className);
            QuartzPlugin.scheduler.pauseJob(jobKey);

            quartzTaskService.updateQuartzTaskState(quartzTaskDTO.getId(), QuartzTaskDTO.STATE_STOP);
            result = JSONResult.success();
        } catch (Exception e) {
            LOGGER.error(e.toString());
            result = JSONResult.fail(201, e.toString());
        }

        return result;
    }

    //立即运行
    @ApiOperation(value = "立即运行")
    @RequestMapping(value = "/trigger", method = RequestMethod.POST)
    public JSONResult trigger(@RequestBody QuartzTaskDTO quartzTaskDTO) {
        JSONResult result = null;
        try {
            QuartzTaskDTO quartzTaskDTOTemp = quartzTaskService.getQuartzTaskById(quartzTaskDTO.getId());
            String className = quartzTaskDTOTemp.getClazz();

            // 立即触发一次
            JobKey jobKey = JobKey.jobKey(className, className);
            QuartzPlugin.scheduler.triggerJob(jobKey);
            result = JSONResult.success();
        } catch (Exception e) {
            LOGGER.error(e.toString());
            result = JSONResult.fail(201, e.toString());
        }
        return result;
    }


    @RequestMapping(value = "/findList", method = RequestMethod.POST)
    public JSONResult findList(@RequestBody Map<Object, Object> map) {
        List<QuartzTaskDTO> list = quartzTaskService.getQuartzTaskList(map);
        JSONResult result = JSONResult.success(list, 200, "");
        return result;
    }

    @RequestMapping(value = "/findPageList", method = RequestMethod.POST)
    public JSONResult findPageList(@RequestBody Map<Object, Object> map) {
        CommPager<QuartzTaskDTO> pageList = quartzTaskService.findPageList(map);
        JSONResult result = JSONResult.success(pageList, 200, "");
        return result;
    }

    @RequestMapping(value = "/getQuartzTaskCount", method = RequestMethod.POST)
    public JSONResult getQuartzTaskCount(@RequestBody Map<Object, Object> map) {
        JSONResult result = JSONResult.success("", 200, "");
        return result;
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JSONResult insertQuartzTask(@RequestBody QuartzTaskDTO quartzTaskDTO) {
        quartzTaskService.insertQuartzTask(quartzTaskDTO);
        JSONResult result = JSONResult.success("", 200, "");
        return result;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JSONResult updateQuartzTask(@RequestBody QuartzTaskDTO quartzTaskDTO) {
        quartzTaskService.updateQuartzTask(quartzTaskDTO);
        JSONResult result = JSONResult.success("", 200, "");
        return result;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public JSONResult delQuartzTaskByPrimaryKey(@RequestBody QuartzTaskDTO quartzTaskDTO) {
        quartzTaskService.delQuartzTaskById(quartzTaskDTO.getId());
        JSONResult result = JSONResult.success("", 200, "");
        return result;
    }

    @RequestMapping(value = "/get", method = RequestMethod.POST)
    public JSONResult getQuartzTaskByPrimaryKey(@RequestBody QuartzTaskDTO quartzTaskDTO) {
        QuartzTaskDTO quartzTaskDTOTemp = quartzTaskService.getQuartzTaskById(quartzTaskDTO.getId());
        JSONResult result = JSONResult.success(quartzTaskDTOTemp, 200, "");
        return result;
    }
}
